<template>
  <div class="wrap">
    <div class="top">
      <div class="hosname">{{ baseMap?.hosname }}</div>
      <div class="line"></div>
      <div class="sub">{{ baseMap?.bigname }}</div>
      <div class="dot">.</div>
      <div class="department">{{ baseMap?.depname }}</div>
    </div>
    <div class="center">
      <p class="title">{{ baseMap?.workDateString }}</p>
      <div class="container">
        <div
          class="item"
          @click="selectSchedule(item)"
          :class="{ selected: currentScheduleId == item.dayOfWeek , active: getShowMessage(item).indexOf('有号') != -1 }"
          v-for="item in bookingSchduleList"
          :key="item.dayOfWeek">
          <div class="item-top">
            <p class="date">{{ item.workDateMd }}</p>
            <p class="week">{{ item.dayOfWeek }}</p>
          </div>
          <div class="bottom">
            {{ getShowMessage(item) }}
          </div>
        </div>
      </div>
      <el-pagination
        v-model:current-page="params.page"
        @current-change="initData"
        layout="prev, pager, next"
        :total="total" />
    </div>
    <p class="bottom-title">
      <el-icon><Sunny /></el-icon>上午号源
    </p>
    <div class="schedule-container">
      <div
        class="detail"
        v-for="item in scheduleList?.filter((item) => item.workTime !== 1)"
        :key="item.id">
        <div class="intro">
          <p class="name">
            <span class="title">{{ item.title }}</span>
            <span class="line">|</span>
            <span class="name">{{ item.docname || '--' }}</span>
          </p>
          <p>擅长：{{ item.skill }}</p>
        </div>
        <div class="amount">￥{{ item.amount }}</div>
        <div class="button">
          <el-button type="primary" @click="goStep2(item)">剩余{{ item.reservedNumber }}</el-button>
        </div>
      </div>
    </div>
    <p class="bottom-title">
      <el-icon><Sunrise /></el-icon>下午号源
    </p>
    <div class="schedule-container">
      <div
        class="detail"
        v-for="item in scheduleList?.filter((item) => item.workTime == 1)"
        :key="item.id">
        <div class="intro">
          <p class="name">
            <span class="title">{{ item.title }}</span>
            <span class="line">|</span>
            <span class="name">{{ item.docname || '--' }}</span>
          </p>
          <p>擅长：{{ item.skill }}</p>
        </div>
        <div class="amount">￥{{ item.amount }}</div>
        <div class="button">
          <el-button type="primary">剩余{{ item.reservedNumber }}</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Sunny, Sunrise } from '@element-plus/icons-vue'
import { reqHospitalSchedule, reqHospitalScheduleDay } from '@/api/hospital'
import {
  SearchParams,
  BookingScheduleList,
  BookingSchedule,
  ScheduleDetail,
  HospitalScheduleBaseMap,
  ScheduleList
} from '@/api/hospital/type'

let $props = defineProps({
  depcode: String
})
let currentScheduleId = ref('')
const $route = useRoute()
const $router = useRouter()
onMounted(() => {
  initData()
})
let params = ref<SearchParams>({
  page: 1,
  limit: 6,
  depcode: $props.depcode as string,
  hoscode: $route.query.hoscode as string
})
let scheduleList = ref<ScheduleList>()
let total = ref(0)
let baseMap = ref<HospitalScheduleBaseMap>()
let bookingSchduleList = ref<BookingScheduleList>([])
const initData = async () => {
  const { data } = await reqHospitalSchedule(params.value)
  baseMap.value = data.baseMap
  bookingSchduleList.value = data.bookingScheduleList || []
  if(bookingSchduleList.value.length > 0){
    selectSchedule(bookingSchduleList.value[0])
  }
  total.value = data.total
}
const getShowMessage = (item: BookingSchedule) => {
  if (item.status == 1) {
    return '即将放号'
  } else if (item.status == 0) {
    return item.reservedNumber == -1 ? '已约满' : `有号（余${item.reservedNumber || 0}）`
  } else {
    return '暂无排号'
  }
}
const selectSchedule = async (item: BookingSchedule) => {
  // 去获取号源信息
  currentScheduleId.value = item.dayOfWeek
  let workDate = item.workDate
  const { data } = await reqHospitalScheduleDay({
    workDate,
    depcode: $props.depcode as string,
    hoscode: $route.query.hoscode as string
  })
  scheduleList.value = data
}
// 确定选择医生,去第二步
const goStep2 = (item: ScheduleDetail) => {
  // 跳转
  $router.push({
    path: '/hospital/register_step2/' + item.id,
    query: {
      hoscode: $route.query.hoscode as string
    }
  })
}
</script>

<style lang="scss" scoped>
.wrap {
  .top {
    display: flex;
    align-items: center;
    color: #7f7f7f;
    .line {
      height: 16px;
      line-height: 16px;
      width: 1px;
      margin: 0 5px;
      background: #7f7f7f;
    }
    .dot {
      margin: 0 5px;
      transform: translateY(-4px);
      font-weight: bold;
    }
  }
  .center {
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      text-align: center;
      line-height: 80px;
      font-size: 17px;
      font-weight: bold;
    }
    .container {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 20px;
      .item {
        min-width: 140px;
        height: 80px;
        margin-right: 16px;
        margin-bottom: 15px;
        border-radius: 4px;
        text-align: center;
        border: 2px solid #e8e8e8;
        cursor: pointer;
        transition: all 0.5s;
        &:hover{
          transform: scale(1.1);
        }
        &.active {
          border: 2px solid #cde5ff; 
          .item-top {
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
            background-color: #e7f6ff;
          }
        }
        &.selected{
          border: 2px solid #5ba5f4; 
          transform: scale(1.1);
        }
        .item-top {
          width: 100%;
          display: flex;
          height: 40px;
          align-items: center;
          justify-content: center;
          background-color: #efefef;
          .date {
            font-size: 15px;
          }
          .week {
            font-size: 13px;
            margin-left: 5px;
          }
        }
        .bottom {
          line-height: 40px;
          color: #7f7f7f;
        }
      }
    }
  }
}
.bottom-title {
  display: flex;
  align-items: center;
  font-weight: bold;
  background-color: #f9fafc;
  line-height: 40px;
  width: 100px;
  padding-left: 10px;
  border-radius: 4px;
  // color: #368eeb;
  color: #444;
  border: 1px solid #b1d2f5;
  font-size: 15px;
  .el-icon {
    margin-right: 5px;
    color: #368eeb;
  }
}
.schedule-container{
  margin: 0 0 20px 0;
  .detail{
    width: 100%;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #e8e8e8;
    padding: 12px 0;
    .intro{
      flex: 1;
      line-height: 30px;
      color: #7f7f7f;
      .name{
        color: #55a6fe;
        span{
          display: inline-block;
          &.line{
            margin: 0 10px;
          }
        }
      }
    }
    .amount {
      width: 100px;
      text-align: left;
    }
    .button{
      width: 100px;
      text-align: right;
      .el-button{
        width: 70px;
      }
    }
  }
}
</style>
